package com.example.demo.controller;

import com.example.demo.common.Result;
import com.example.demo.model.User;
import org.springframework.web.bind.annotation.*;
import sun.dc.DuctusRenderingEngine;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * 登录
     * @param user 提交的用户名和密码
     * @param httpSession 提交的
     * @return 成功信息
     */
    @PostMapping("/login")
    public Result login(User user, HttpSession httpSession) {
        if (user != null && (user.getPassword()).equals("123")) {
            // 将用户数据存储到session中
            httpSession.setAttribute("user",user.getUsername());
            System.out.println(httpSession);
            return Result.success("登录成功");
        } else {
            return Result.failed();
        }
    }

    /**
     * 获取当前登录的用户名
     * @return 用户名
     */
    @GetMapping("/getusername")
    public Result login(HttpSession httpSession) {
        String username = (String) httpSession.getAttribute("user");
        return Result.success(username);
    }

}
